<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdminAction
 *
 * @author Administrator
 */
import('@.Service.ActionService');
import('@.Service.ConfigService');
import('@.Filter.SecurityAction');
import('@.Util.ConstantUtil');
vendor('Snoopy.Snoopy#class');
vendor('simplehtmldom.simple_html_dom');

class AdminAction extends SecurityAction {

    function index() {
        $actionService = new ActionService();
        $menu = $actionService->listAll();

        Session::set(SYSTEM_MENU, json_encode($menu));

        $this->assign('content', 'index');
        $this->display('Admin:global');
    }

    //跳转发送  PushMessage 页面
    function pushMessagePage() {
        $configService = new ConfigService();

        $mobile = $configService->getConfig("push_music");

        $music = $mobile['value'];

        $musices = split(',', $music);


        $this->assign('musices', $musices);
        $this->assign('mobile', $mobile);
        $this->assign('content', 'Mobile:pushMessage');
        $this->display('Admin:global');
    }

    function pushMessage() {
//        $URL = ConstantUtil::$PUSH_MESSAGE_HOST;
//        $post_data['text'] = $_POST['message'];
//        $post_data['music'] = $_POST['push_music'];
//
//        $referrer = "";
//// parsing the given URL
//        $URL_Info = parse_url($URL);
//// Building referrer
//        if ($referrer == "") // if not given use this script as referrer
//            $referrer = $_SERVER["SCRIPT_URI"];
//
//// making string from $data
//        foreach ($post_data as $key => $value)
//            $values[] = "$key=" . urlencode($value);
//
//        $data_string = implode("&", $values);
//// Find out which port is needed - if not given use standard (=80)
//        if (!isset($URL_Info["port"]))
//            $URL_Info["port"] = 8080;
//// building POST-request:
//        $request.="POST " . $URL_Info["path"] . " HTTP/1.1\n";
//        $request.="Host: " . $URL_Info["host"] . "\n";
//        $request.="Referer: $referrer\n";
//        $request.="Content-type: application/x-www-form-urlencoded\n";
//        $request.="Content-length: " . strlen($data_string) . "\n";
//        $request.="Connection: close\n";
//        $request.="\n";
//        $request.=$data_string . "\n";
//        $fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
//        
//        dump($URL_Info["port"]);return;
//        fputs($fp, $request);
//        while (!feof($fp)) {
//            $result .= fgets($fp, 128);
//        }
//        fclose($fp);
        $url = ConstantUtil::$PUSH_MESSAGE_HOST;
        $ch = curl_init();
        $data = array('text' => $_POST['message'], 'music' => $_POST['push_music']);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_exec($ch);


        $this->assign("push_ok", "已发送Push消息");

        $this->pushMessagePage();
    }

}

?>
